import React, { useEffect, useState } from 'react';
import { Select, Input, DatePicker, Button, Modal, notification, Upload } from 'antd';
import moment from "moment";

import City from "../../components/City"
import Store from "../../components/Store"

import http from "../../request";
import { BASE_URL, UPLOAD_URL } from "../../request/api";

import { store } from "../../store";
import { enSplit } from "../../common/common";

const { Option } = Select;

const Edit = (props) => {
    const { sethange, close, refresh, id, info, setEstateShow, setEstateId, setEstateTab, setOpe, staffinfo, setj } = props

    const {house1, house2} = staffinfo

    const [render, setRender] = useState(!1);
    const [form, setForm] = useState({
        city: undefined,
        region: undefined,
        area: undefined,
        estate_id: undefined,
        building_id: undefined,
        unit: '',
        floor: '',
        total_floor: '',
        num_room: undefined,
        num_balcony: undefined,
        num_office: undefined,
        num_toilet: undefined,
        is_network_room: 0,
        room_type: '普通房',
        house_pattern: undefined,
        house_number: '',
        purpose: undefined,
        house_area: '',
        suite: '',
        orientation: undefined,
        type: undefined,
        house_years: undefined,
        transaction: undefined,
        entrust: '',
        entrust_type: undefined,
        price: [],
        source: undefined,
        state: undefined,
        deliver: '',
        entrust_num: '',
        rental: [],
        guide_price: '',
        current_state: undefined,
        furniture: undefined,
        property_right: undefined,
        payment: undefined,
        elevator: undefined,
        elevator_households: undefined,
        hardcover: undefined,
        electronics: undefined,
        certificates: undefined,
        vassal: undefined,
        house_view: undefined,
        house_view_num: '',
        matching: [],
        remark: ''
    });
    const [estateList, setEstateList] = useState([]);
    const [buildList, setBuildList] = useState([]);
    const [owner, setOwner] = useState([{ name: '', mobile: '' }]);
    const [staff, setStaff] = useState([{ store_id: undefined, staff_id: undefined, memo: '' }])
    const [imgList, setImgList] = useState([]);
    const [followList, setFollowList] = useState([]);
    const [fileList, setFileList] = useState([])
    const [preview, setPreview] = useState({ show: !1, content: '' })
    const [upload, setUpload] = useState({ show: !1 })
    const [lock, setLock] = useState(!1)
    const uploadConfig = {
        name: "file",
        accept: 'image/*',
        fileList: fileList,
        listType: 'picture',
        // showUploadList: false,
        multiple: true,
        action: UPLOAD_URL,
        headers: {
            token: store.getState().userInfo.token
        },
        onChange: ({ fileList }) => {
            setFileList(fileList)
        },
        onPreview: { filePreview }
    }

    // 提示信息
    const [ismessage, setismessage] = useState(!1)
    const [ismessage2,setismessage2] = useState(!1)
    const [ismessage3,setismessage3] = useState(!1)

    const [change,setchange] = useState(!1)

    useEffect(() => {
        id && detail();
        !id && setRender(!0);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    
    let time

    return (
        <React.Fragment>
            {render && <div className="House-Edit Edit page">
                <div className="container">
                    {/* <div className="part-title">基本信息</div> */}
                    <div className="u-flex">
                        <div className="label">地址</div>
                        <City value={[form.city, form.region, form.area]} onChange={cityChange} className="u-m-l-10" />
                        <Select
                            placeholder="楼盘字典"
                            value={form.estate_id}
                            onChange={estateChange}
                            showSearch
                            onSearch={buildByName}
                            filterOption={false}
                            className=""
                            style={{ width: 150 }}
                        >
                            {estateList.map(e => <Option value={e.id} key={e.id}>{e.name}</Option>)}
                        </Select>
                        <img src={require('../../assets/icon/icon-40.png').default} className=" u-btn" onClick={() => {
                            info && setEstateTab()('1')
                            info && setEstateShow()(!0)
                            info && setEstateId()(info.estate_id)
                        }} style={{ width: 25 }} alt=""/>
                        <div className="label u-m-l-10">建房年代</div>
                        <Input placeholder="建房年代" value={form.house_years} onChange={e => formChange('house_years', e.target.value)} className="u-m-l-5" style={{ width: 70 }} />
                        <div className="label u-m-l-10">房源编号</div>
                        <Input placeholder="BH8888" value={form.house_number} onChange={e => formChange('house_number', e.target.value)} disabled className="u-m-l-5" style={{ width: 100 }} />
                        {/*<img className="u-btn u-m-l-5 u-m-b-10" src={require('../../assets/icon/icon-21.png').default} alt=""/>*/}
                        {(house1?.position?.value ?? 1) ?
                           <>
                            <div className="label u-m-l-10">栋座/位置</div>
                        <Select placeholder="栋座/位置" value={form.building_id} onChange={buildChange} className="u-m-l-5" style={{ width: 100 }}>
                            {buildList.map(e => <Option value={e.id} key={e.id}>{e.name}</Option>)}
                        </Select>
                           </>
                        : ''}
                    </div>
                    <div className="u-flex u-m-t-10">
                        <div className="label">单元</div>
                        <Input placeholder="单元" value={form.unit} onChange={e => formChange('unit', e.target.value)} className="u-m-l-5" style={{ width: 60 }} />
                        {(house1?.floor?.value ?? 1) ?
                            <>
                            <div className="label u-m-l-10">楼层</div>
                        <Input placeholder="楼层" value={form.floor} onChange={e => formChange('floor', e.target.value)} className="u-m-l-5" style={{ width: 60 }} />
                            </>
                        : ''}
                       {(house1?.room_num?.value ?? 1) ?
                        <>
                        <div className="label u-m-l-10">房号</div>
                        <Input placeholder="房号" value={form.room_num} onChange={e => formChange('room_num', e.target.value)} className="u-m-l-5" style={{ width: 60 }} />
                        </>
                        : ''}
                        {(house1?.total_floor?.value ?? 1) ?
                            <>
                            <div className="label u-m-l-10">总层</div>
                        <Input placeholder="总层" value={form.total_floor} onChange={e => formChange('total_floor', e.target.value)} className="u-m-l-5" style={{ width: 60 }} />
                            </>
                        : ''}
                        {(house1?.room_type?.value ?? 1) ?
                            <>
                            <div className="label u-m-l-10">房型</div>
                        <Select allowClear value={form.num_room} onChange={e => formChange('num_room', e)} className="u-m-l-5" style={{ width: 60 }}>
                            {
                                new Array(20).fill(1).map((e, i) => <Option value={i + 1} key={i}>{i + 1} </Option>)
                            }
                        </Select>
                        <div className="label u-m-l-5">房</div>
                        <Select allowClear value={form.num_balcony} onChange={e => formChange('num_balcony', e)} className="u-m-l-10" style={{ width: 60 }}>
                            {
                                new Array(20).fill(1).map((e, i) => <Option value={i + 1} key={i}>{i + 1} </Option>)
                            }
                        </Select>
                        <div className="label u-m-l-5">厅</div>
                        <Select allowClear value={form.num_office} onChange={e => formChange('num_office', e)} className="u-m-l-10" style={{ width: 60 }}>
                            {
                                new Array(20).fill(1).map((e, i) => <Option value={i + 1} key={i}>{i + 1} </Option>)
                            }
                        </Select>
                        <div className="label u-m-l-5">卫</div>
                        <Select allowClear value={form.num_toilet} onChange={e => formChange('num_toilet', e)} className="u-m-l-10" style={{ width: 60 }}>
                            {
                                new Array(20).fill(1).map((e, i) => <Option value={i + 1} key={i}>{i + 1} </Option>)
                            }
                        </Select>
                        <div className="label u-m-l-5">阳台</div>
                            </>
                        : ''}
                        {/*<Checkbox checked={form.is_network_room === 1} onChange={e => formChange('is_network_room', e.target.checked?1:0)} className="u-m-l-20">网络房</Checkbox>*/}
                        <Select allowClear value={form.room_type} onChange={e => formChange('room_type', e)} className="u-m-l-10" style={{ width: 90 }}>
                            <Option value="普通房">普通房</Option>
                            {(house1?.yzfsz?.value ?? 1) ? <Option value="优质房">优质房</Option> : ''}
                            {(house1?.jjfsz?.value ?? 1) ? <Option value="聚焦房">聚焦房</Option> : ''}
                        </Select>
                        <div className="label u-m-l-10">用途</div>
                        <Select placeholder="用途" allowClear value={form.purpose} onChange={e => formChange('purpose', e)} className="u-m-l-5" style={{ width: 80 }}>
                            <Option value="住宅">住宅</Option>
                            <Option value="商住">商住</Option>
                            <Option value="商铺">商铺</Option>
                            <Option value="网点">网点</Option>
                            <Option value="写字楼">写字楼</Option>
                            <Option value="厂房">厂房</Option>
                            <Option value="写厂">写厂</Option>
                            <Option value="铺厂">铺厂</Option>
                            <Option value="仓库">仓库</Option>
                            <Option value="地皮">地皮</Option>
                            <Option value="车位">车位</Option>
                            <Option value="其他">其他</Option>
                        </Select>
                    </div>
                    <div className="u-flex u-m-t-10">
                        {(house1?.orientation?.value ?? 1) ? 
                        <>
                        <div className="label ">朝向</div>
                        <Select placeholder="朝向" allowClear value={form.orientation} onChange={e => formChange('orientation', e)} className="u-m-l-5" style={{ width: 80 }}>
                            <Option value="未知">未知</Option>
                            <Option value="东">东</Option>
                            <Option value="南">南</Option>
                            <Option value="西">西</Option>
                            <Option value="北">北</Option>
                            <Option value="南北">南北</Option>
                            <Option value="东南">东南</Option>
                            <Option value="西南">西南</Option>
                            <Option value="东北">东北</Option>
                            <Option value="西北">西北</Option>
                            <Option value="东西">东西</Option>
                        </Select>
                        </> 
                        : ''}
                        {(house1?.house_area?.value ?? 1) ?
                           <>
                            <div className="label u-m-l-10">面积</div>
                        <Input placeholder="面积" value={form.house_area} onChange={e => formChange('house_area', e.target.value, -1)} className="u-m-l-5" style={{ width: 60 }} />
                           </>
                        : ''}
                        <div className="label u-m-l-5">㎡</div>
                        <div className="label u-m-l-10">套内</div>
                        <Input placeholder="套内" value={form.suite} onChange={e => formChange('suite', e.target.value)} className="u-m-l-5" style={{ width: 60 }} />
                        <div className="label u-m-l-5">㎡</div>
                        <div className="label u-m-l-10">类型</div>
                        <Select placeholder="类型" allowClear value={form.type} onChange={e => formChange('type', e)} className="u-m-l-5" style={{ width: 100 }}>
                            <Option value="多层">多层</Option>
                            <Option value="高层">高层</Option>
                            <Option value="小高层">小高层</Option>
                            <Option value="多层复式">多层复式</Option>
                            <Option value="高层复式">高层复式</Option>
                            <Option value="多层跃式">多层跃式</Option>
                            <Option value="高层跃式">高层跃式</Option>
                            <Option value="独立别墅">独立别墅</Option>
                            <Option value="联体别墅">联体别墅</Option>
                            <Option value="双拼别墅">双拼别墅</Option>
                            <Option value="叠加别墅">叠加别墅</Option>
                            <Option value="围院别墅">围院别墅</Option>
                            <Option value="裙楼">裙楼</Option>
                            <Option value="四合院">四合院</Option>
                        </Select>
                        {(house1?.transaction?.value ?? 1) ?
                            <>
                            <div className="label u-m-l-10">交易</div>
                        <Select placeholder="交易" allowClear value={form.transaction} onChange={e => formChange('transaction', e)} className="u-m-l-5" style={{ width: 80 }}>
                            <Option value="出售">出售</Option>
                            <Option value="出租">出租</Option>
                            <Option value="租售">租售</Option>
                        </Select>
                            </>
                        : ''}
                        {house1?.price?.value ?? 1 ?
                            <>
                            <div className="label u-m-l-10">售价</div>
                        <Input placeholder="金额" value={form.transaction === '出租' ? '' : form.price[0] === 'NaN' ? 0 : form.price[0] ? form.price[0] : 0} className="u-m-l-10" style={{ width: 80 }} />
                        <div className="label u-m-l-5">万元</div>
                        <Input placeholder="0.00" value={form.transaction === '出租' ? '' : form.price[1]} onChange={e => form.transaction === '出租' ? '' : formChange('price', e.target.value, 1)
                        } className="u-m-l-5" style={{ width: 70 }} />
                        <div className="label u-m-l-5">/㎡</div>
                        <div className="label u-m-l-10">底价</div>
                        <Input placeholder="金额" value={form.transaction === '出租' ? '' : form.price[2]} onChange={e => form.transaction === '出租' ? '' : formChange('price', e.target.value, 2)} className="u-m-l-5" style={{ width: 70 }} />
                        <div className="label u-m-l-5">万元</div>
                            </>
                        : ''}
                    </div>
                    
                    {/* <Divider /> */}
                    {/* <div className="part-title">交易信息</div> */}
                    <div className="u-flex u-m-t-10">
                        {(house1?.source?.value ?? 1) ?
                            <>
                            <div className="label">来源</div>
                        <Select placeholder="来源" allowClear value={form.source ?? undefined} onChange={e => formChange('source', e)} className="u-m-l-5" style={{ width: 80 }}>
                            <Option value="来电">来电</Option>
                            <Option value="来访">来访</Option>
                            <Option value="中介">中介</Option>
                            <Option value="朋友">朋友</Option>
                            <Option value="广告">广告</Option>
                            <Option value="扫街">扫街</Option>
                            <Option value="网络">网络</Option>
                            <Option value="搜房">搜房</Option>
                            <Option value="口碑">口碑</Option>
                        </Select>
                            </>
                        : ''}
                        {(house1?.state?.value ?? 1) ?
                            <>
                            <div className="label u-m-l-10">状态</div>
                        <Select placeholder="状态" allowClear value={form.state} onChange={e => formChange('state', e)} className="u-m-l-5" style={{ width: 80 }}>
                            <Option value="有效">有效</Option>
                            <Option value="特定">特定</Option>
                            <Option value="已售">已售</Option>
                            <Option value="已租">已租</Option>
                            <Option value="我售">我售</Option>
                            <Option value="我租">我租</Option>
                            <Option value="暂缓">暂缓</Option>
                            <Option value="未知">未知</Option>
                            <Option value="无效">无效</Option>
                        </Select>
                            </>
                        : ''}
                        {(house1?.price?.value ?? 1) ?
                            <>
                            <div className="label u-m-l-10">租价</div>
                        <Input placeholder="金额" value={form.transaction === '出售' ? '' : form.rental[0]} onChange={e => form.transaction === '出售' ? '' : formChange('rental', e.target.value, 0)} className="u-m-l-5" style={{ width: 70 }} />
                        <div className="label u-m-l-5">元/月</div>
                        <Input placeholder="0.00" value={form.transaction === '出售' ? '' : (form.rental[1] * 1).toFixed(2) === 'NaN' ? 0 : (form.rental[1] * 1).toFixed(2)} className="u-m-l-5" style={{ width: 70 }} />
                        <div className="label u-m-l-5">/㎡</div>
                        <div className="label u-m-l-10">底价</div>
                        <Input value={form.transaction === '出售' ? '' : form.rental[2]} onChange={e => form.transaction === '出售' ? '' : formChange('rental', e.target.value, 2)} className="u-m-l-5" style={{ width: 70 }} />
                        <div className="label u-m-l-5">元/月</div>
                        <div className="label u-m-l-10">指导价</div>
                        <Input placeholder="0.00" value={form.guide_price} onChange={e => formChange('guide_price', e.target.value)} className="u-m-l-5" style={{ width: 70 }} />
                            </>
                        : ''}
                        <div className="label u-m-l-10">委托</div>
                        <DatePicker className="u-m-l-5" value={form.entrust ? moment(form.entrust) : ''} onChange={(e, es) => formChange('entrust', es)} placeholder="委托日期" style={{ width: 120 }} />
                    </div>
                    {/* <div className="u-flex u-m-t-20">
                        
                    </div>
                    <Divider /> */}
                    {/* <div className="part-title">详细信息</div> */}
                    <div className="u-flex u-m-t-10">
                        {(house1?.elevator?.value ?? 1) ?
                            <>
                            <div className="label">电梯</div>
                        <Select placeholder="电梯" allowClear value={form.elevator} onChange={e => formChange('elevator', e)} className="u-m-l-5" style={{ width: 80 }}>
                            <Option value="电梯">电梯</Option>
                            <Option value="楼梯">楼梯</Option>
                        </Select>
                            </>
                        : ''}
                        <Select placeholder="几梯几户" allowClear value={form.elevator_households} onChange={e => formChange('elevator_households', e)} style={{ width: 100 }}>
                            <Option value="一梯两户">一梯两户</Option>
                            <Option value="一梯三户">一梯三户</Option>
                            <Option value="两梯三户">两梯三户</Option>
                            <Option value="两梯五户">两梯五户</Option>
                            <Option value="两梯六户">两梯六户</Option>
                        </Select>
                    </div>
                    <div className="u-grid u-m-t-10 u-border-top">
                        <div className="u-p-t-5 u-p-b-5" style={{ width: 525 }}>
                            {/* <div className="part-title">业主信息</div> */}
                            {(house1?.yz?.value ?? 1) ? 
                                <div className="u-flex u-flex-wrap">
                                {
                                    owner.map((e, i) => {
                                        return (
                                            <div className="u-flex u-m-b-10" key={i}>
                                                <div className="label">业主{i + 1}</div>
                                                <Input placeholder="业主" value={e.name} onChange={e => ownerChange(i, 'name', e.target.value)} className="u-m-l-5" style={{ width: 70 }} />
                                                <Input placeholder="联系方式" value={e.mobile} onChange={e => ownerChange(i, 'mobile', e.target.value)} className="u-m-l-5" style={{ width: 110 }} />
                                            </div>
                                        )
                                    })
                                }
                                {
                                    owner.length < 6 && (
                                        <Button className="u-m-b-10" size="small" type="primary" onClick={ownerAdd}>
                                            <div className="u-flex">
                                                <img src={require('../../assets/icon/icon-29.png').default} alt="" />
                                                <span className="u-m-l-5 u-text-nowrap">业主新增</span>
                                            </div>
                                        </Button>
                                    )
                                }
                            </div>
                            : ''}
                        </div>
                        <div className="u-p-l-5 u-p-b-5 u-border-left" style={{ width: 525,height:135 }}>
                            {/* <div className="part-title">员工信息</div> */}
                            {
                                staff.map((e, i) => {
                                    return (
                                        <div className="u-flex u-m-t-5" key={i}>
                                            <Store value={[e.store_id, e.staff_id]} onChange={(store_id, staff_id) => storeChange(i, store_id, staff_id)} />
                                            <Input placeholder="备注" value={e.memo} onChange={e => staffChange(i, e.target.value)} className="u-m-l-10" style={{ width: 240 }} />
                                        </div>
                                    )
                                })
                            }
                            {
                                staff.length < 3 && (
                                    <Button className="u-m-t-5" size="small" type="primary" onClick={staffAdd}>
                                        <div className="u-flex">
                                            <img src={require('../../assets/icon/icon-29.png').default} alt="" />
                                            <span className="u-m-l-5 u-text-nowrap">新增员工</span>
                                        </div>
                                    </Button>
                                )
                            }
                        </div>
                        
                        {/* <div className="u-p-t-20 u-p-b-20 u-p-l-20 u-border-top">
                            <div className="part-title">跟进</div>
                            {
                                followList.length > 0 ? (
                                    <>
                                        <div className="u-m-t-10">
                                            {
                                                followList.map((e, i) => {
                                                    return (
                                                        <div className="follow-item u-flex" key={i}>
                                                            <div className="u-p-5 u-line-1" style={{ width: 280 }}>{e.content}</div>
                                                            <div className="u-p-5">{e.follow_up_staff_data?.nickname ?? ''}</div>
                                                            <div className="u-p-5">{e.state === '1' ? '已审' : '未审'}</div>
                                                            <div className="u-p-5">{e.follow_up_time}</div>
                                                        </div>
                                                    )
                                                })
                                            }
                                        </div>
                                        <div className="u-text-right u-m-t-20">
                                            <Button type="primary" size="small" onClick={() => modalShow(setFollow)}>添加跟进</Button>
                                        </div>
                                    </>
                                ) : (
                                    <div className="u-flex u-row-center u-m-t-20">
                                        <div className="upload-item u-flex u-flex-col u-row-center">
                                            <img src={require('../../assets/icon/icon-23.png').default} alt="" />
                                            <Button className="u-m-t-20" type="primary" size="small" onClick={() => modalShow(setFollow)}>添加跟进</Button>
                                        </div>
                                    </div>
                                )
                            }
                        </div> */}
                    </div>
                    {(house2?.scfytp?.value ?? '') !== '无' ?
                        <div className="u-p-t-10 u-p-b-10 u-border-top" style={{width:1050}}>
                            {/* <div className="part-title">图片上传</div> */}
                            {
                                imgList.length > 0 ? (
                                    <>
                                        <div className="u-flex u-flex-wrap" style={{ maxHeight: 400, overflowY: 'auto' }}>
                                            {
                                                imgList.map((img, ind) => (
                                                    <div className="img-item" key={ind}>
                                                        <img className="u-btn img" src={BASE_URL + img.content} alt="" onClick={() => filePreview(img)} />
                                                        <img className="u-btn img-del" src={require('../../assets/icon/icon-24.png').default} alt="" onClick={() => { fileDel(ind) }} />
                                                    </div>
                                                ))
                                            }
                                        </div>
                                        <div className="u-p-20 u-text-right">
                                            <Button className="u-m-l-20" type="primary" size="small" onClick={() => modalShow(setUpload)}>上传图片</Button>
                                        </div>
                                    </>
                                ) : (
                                    <div className="u-flex u-row-center u-m-t-20">
                                        <div className="upload-item u-flex u-flex-col u-row-center">
                                            <img src={require('../../assets/icon/icon-22.png').default} alt="" />
                                            <Button className="u-m-t-20" type="primary" size="small" onClick={() => modalShow(setUpload)}>上传图片</Button>
                                        </div>
                                    </div>
                                )
                            }
                        </div>
                        : ''}
                    <div className="u-text-center u-p-t-20">
                        <Button type="primary" loading={lock} onClick={()=>{
                            if((form.rental[0]*1 < form.rental[2]*1) || (form.state === '有效') || change ){
                                if(form.rental[0]*1 < form.rental[2]*1) setismessage(!0)
                                if(form.state === '有效') setismessage2(!0)
                                change && setismessage3(!0)
                            }else{
                                save()
                            }
                        }}>保存</Button>
                        <Button className="u-m-l-20" type="primary" ghost onClick={() => close(!1)}>取消</Button>
                        <Button className="u-m-l-20" onClick={() => {setOpe()(!0)}}>跟进记录</Button>
                    </div>
                </div>
            </div>}
            <Modal visible={upload.show} maskClosable={false} width={880} onOk={fileConfirm} onCancel={fileCancel}>
                <div className="Modal Building-Edit">
                    <Upload {...uploadConfig} className="upload-list-inline">
                        <Button type="primary" size="small" className="u-flex">
                            <img src={require('../../assets/icon/icon-9.png').default} alt="" />
                            <span className="u-m-l-5">选择图片</span>
                        </Button>
                    </Upload>
                </div>
            </Modal>
            <Modal visible={preview.show} width={1000} footer={null} onCancel={() => { setPreview({ show: !1 }) }}>
                <img src={preview.content} alt="" style={{ width: '100%' }} />
            </Modal>
            {/* <Modal title="添加跟进" width={370} visible={follow.show} onOk={followFormConfirm} onCancel={() => modalHide(setFollow)}>
                <div className="Modal">
                    {mask.show && (
                        <div className="mask">
                            <Spin size="large" tip={mask.text} />
                        </div>
                    )}
                    <div className="u-flex">
                        <div className="label">时间方式</div>
                        <DatePicker placeholder="时间" value={followForm.follow_up_time ? moment(followForm.follow_up_time) : ''} onChange={(e, es) => followFormChange('follow_up_time', es)} className="u-m-l-10" style={{ width: 120 }} />
                        <Select placeholder="跟进方式" value={followForm.follow_up_mode} onChange={e => followFormChange('follow_up_mode', e)} className="u-m-l-10" style={{ width: 120 }}>
                            <Option value="去电">去电</Option>
                            <Option value="来电">来电</Option>
                            <Option value="看房">看房</Option>
                            <Option value="来访">来访</Option>
                            <Option value="拜访">拜访</Option>
                            <Option value="派单">派单</Option>
                            <Option value="短信">短信</Option>
                            <Option value="邮件">邮件</Option>
                            <Option value="信函">信函</Option>
                            <Option value="申请">申请</Option>
                            <Option value="修改">修改</Option>
                            <Option value="保留">保留</Option>
                        </Select>
                        <div className="label u-m-l-10">下次提醒</div>
                        <DatePicker placeholder="时间" value={followForm.next_remind?moment(followForm.next_remind):''} onChange={(e, es) => followFormChange('next_remind', es)} className="u-m-l-10" style={{width: 120}} />
                    </div>
                    <div className="u-flex u-m-t-20">
                        <div className="label">跟进员工</div>
                        <Store value={[followForm.follow_up_store_id, followForm.follow_up_staff_id]} onChange={followStaffChange} className="u-m-l-10" />
                        <div className="label u-m-l-10">提醒范围</div>
                        <Store value={[followForm.remind_store_id, followForm.remind_staff_id]} onChange={remindStaffChange} className="u-m-l-10" />
                    </div>
                    <div className="u-flex u-m-t-20">
                        <div className="label">去电业主</div>
                        <Input placeholder="业主" value={followForm.owner} onChange={e => followFormChange('owner', e.target.value)} className="u-m-l-10" style={{ width: 120 }} />
                        <Select placeholder="跟进类型" value={followForm.follow_up_type} onChange={e => followFormChange('follow_up_type', e)} className="u-m-l-10" style={{ width: 120 }}>
                            <Option value="普通跟进">普通跟进</Option>
                            <Option value="重点跟进">重点跟进</Option>
                            <Option value="专属跟进">专属跟进</Option>
                        </Select>
                        <div className="label u-m-l-10">提醒内容</div>
                        <Input placeholder="提醒内容" value={followForm.remind_cont} onChange={e => followFormChange('remind_cont', e.target.value)} className="u-m-l-10" style={{width: 250}} />
                    </div>
                    <div className="u-flex u-m-t-20">
                        <div className="label">跟进内容</div>
                        <TextArea placeholder="跟进内容" value={followForm.content} onChange={e => followFormChange('content', e.target.value)} className="u-m-l-10" style={{ width: 250, height: 105 }} />
                        <div className="u-m-l-10">
                           <div className="u-flex">
                               <div className="label">处理员工</div>
                               <Store value={[followForm.handle_store_id, followForm.handle_staff_id]} onChange={handleStaffChange} className="u-m-l-10" />
                           </div>
                           <div className="u-flex u-m-t-20">
                               <div className="label">经理点评</div>
                               <TextArea placeholder="" value={followForm.common} onChange={e => followFormChange('common', e.target.value)} className="u-m-l-10" style={{width: 250}}/>
                           </div>
                        </div>
                    </div>
                    <div className="tip u-m-t-20 u-m-l-10">
                        <span className="tip-icon">*</span>
                        <span>跟进保存后不可修改，请确认内容后再保存</span>
                    </div>
                </div>
            </Modal> */}
            
            <Modal title="提示信息" visible={ismessage} onOk={()=>{
                setismessage(!1)
                save()
                }} onCancel={()=>{setismessage(!1)}}>
                <div className="u-flex u-col-center">
                    <img src={require('../../assets/icon/icon-41.png').default} className="u-m-l-10 u-btn" style={{ width: 50 }} alt=""/>
                    <p className="u-m-l-10" style={{fontSize:18,fontWeight:500}}>【租价】小于【底价】！是否继续？</p>
                </div>
            </Modal>
            <Modal title="提示信息" visible={ismessage2} onOk={()=>{
                setismessage2(!1)
                !(form.rental[0]*1 < form.rental[2]*1) && save()
            }} onCancel={()=>{
                setismessage2(!1)
                setismessage(!1)
                }}>
                <div className="u-flex u-col-center">
                    <img src={require('../../assets/icon/icon-41.png').default} className="u-m-l-10 u-btn" style={{ width: 50 }} alt=""/>
                    <p className="u-m-l-10" style={{fontSize:18,fontWeight:500}}>房源改为【有效】状态可自动修改【委托日期】为今天。是否确定要修改？</p>
                </div>
            </Modal>
            <Modal title="提示信息" visible={ismessage3} onOk={()=>{
                setismessage3(!1)
                !(form.rental[0]*1 < form.rental[2]*1) && !(form.state === '有效') && save()
            }} onCancel={()=>{
                setismessage3(!1)
                setismessage(!1)
                setismessage2(!1)
                }}>
                <div className="u-flex u-col-center">
                    <img src={require('../../assets/icon/icon-41.png').default} className="u-m-l-10 u-btn" style={{ width: 50 }} alt=""/>
                    <p className="u-m-l-10" style={{fontSize:18,fontWeight:500}}>房源改为【交易-状态-价格】，是否发【留言】通知房源员工？</p>
                </div>
            </Modal>
        </React.Fragment>
    );
    function detail() {
        http('house.one', {
            ids: id
        }).then(house => {
            if (house.code === 1) {
                house.data.price = house?.data?.price?.split(',') ?? ''
                house.data.rental = house?.data?.rental?.split(',') ?? ''
                house.data.matching = house?.data?.matching?.split(',') ?? ''
                http('estate.index', {
                    filter: JSON.stringify({ city: house.data.city, region: house.data.region, area: house.data.area }),
                    op: JSON.stringify({ city: '=', region: '=', area: '=' })
                }).then(estate => {
                    if (estate.code === 1) {
                        setEstateList(estate.data.rows)
                        http('building.index', {
                            filter: JSON.stringify({ estate_id: house.data.estate_id }),
                            op: JSON.stringify({ estate_id: '=' })
                        }).then(build => {
                            if (build.code === 1) {
                                setBuildList(build.data.rows)
                                setRender(!0)
                            }
                        })
                    }
                })
                setOwner(house.data.house_owner_data || [])
                setStaff(house.data.house_staff_data || [])
                setImgList(house.data.house_resources_data || [])
                setFollowList(house.data.house_follow_up_data || [])
                setForm(house.data)
            }
        }).catch(err => console.log(err))
    }
    // 选择城市
    function cityChange(city, region, area) {
        let newForm = JSON.parse(JSON.stringify(form))
        newForm.city = city
        newForm.region = region
        newForm.area = area
        newForm.estate_id = undefined
        newForm.building_id = undefined
        newForm.unit = ''
        newForm.total_floor = ''
        newForm.house_years = ''
        setForm(newForm)
        http('estate.index', {
            filter: JSON.stringify({ city, region, area }),
            op: JSON.stringify({ city: '=', region: '=', area: '=' })
        }).then(res => {
            if (res.code === 1) {
                setEstateList(res.data.rows)
                setBuildList([])
            }
        }).catch(err => console.log(err))
    }
    // 选择楼盘
    function estateChange(e) {
        let newForm = JSON.parse(JSON.stringify(form))
        newForm.estate_id = e
        newForm.building_id = undefined
        newForm.unit = ''
        newForm.total_floor = ''
        let ind = estateList.findIndex(s => s.id === e)
        console.log(estateList[ind]);
        newForm.house_years = estateList[ind].property_right
        newForm.city = estateList[ind].city
        newForm.region = estateList[ind].region
        newForm.area = estateList[ind].area
        setForm(newForm)
        http('building.index', {
            filter: JSON.stringify({ estate_id: e }),
            op: JSON.stringify({ estate_id: '=' })
        }).then(res => {
            if (res.code === 1) {
                setBuildList(res.data.rows)
            }
        })
    }
    // 选择栋座
    function buildChange(e) {
        let newForm = JSON.parse(JSON.stringify(form))
        newForm.building_id = e
        let ind = buildList.findIndex(b => b.id === e)
        newForm.unit = buildList[ind].unit
        newForm.total_floor = buildList[ind].total_layers
        setForm(newForm)
    }
    // 房源-编辑
    function formChange(name, value, ind = null) {
        let newForm = JSON.parse(JSON.stringify(form))
        if (ind === -1) {
            newForm['price'][0] = calculation(parseFloat(form.price[1]), value, 'price') + ''
            newForm['rental'][1] = calculation(parseFloat(form.rental[0]), value, 'rental') + ''
            newForm[name] = value
            setForm(newForm)
            return
        }
        if(name === 'transaction' || name === 'state' || name === 'price') setchange(!0)

        if (ind !== null) {
            newForm[name][ind] = value
            if (ind === 1 && name === 'price') { newForm[name][0] = calculation(value, form.house_area, 'price') + '' }
            if (ind === 0 && name === 'rental') { newForm[name][1] = calculation(value, form.house_area, 'rental') + '' }
        } else {
            newForm[name] = value
        }
        setForm(newForm)
    }
    // 房源-提交
    function save() {
        setLock(!0)
        let newForm = enSplit(form, { price: ',', rental: ',', matching: ',' })
        newForm.house_owner = owner
        newForm.house_staff = staff
        newForm.house_resources = imgList
        newForm.house_follow_up = followList
        newForm.screen_price = form.price[0]
        let url = newForm?.id ? 'house.edit' : 'house.add'
        if (newForm.transaction === '出售') {
            newForm.rental = []
        }
        if (newForm.transaction === '出租') newForm.price = []
        http(url, {
            row: newForm
        }).then(res => {
            if (res.code === 1) {
                notification.success({
                    message: res.msg || '添加成功',
                    duration: 1.5,
                    onClose: () => {
                        close(!1)
                        sethange(!1)
                        refresh()
                    }
                })
            } else {
                notification.error({
                    message: res.msg || '添加失败，请重试'
                })
            }
            setLock(!1)
        })
    }
    // 业主编辑
    function ownerChange(ind, name, value) {
        let newOwner = JSON.parse(JSON.stringify(owner))
        newOwner[ind][name] = value
        setOwner(newOwner)
    }
    // 业主-新增
    function ownerAdd() {
        let newOwner = JSON.parse(JSON.stringify(owner))
        newOwner.push({ name: '', mobile: '' })
        setOwner(newOwner)
    }
    // 员工-门店
    function storeChange(ind, store_id, staff_id) {
        let newStaff = JSON.parse(JSON.stringify(staff))
        newStaff[ind].store_id = store_id
        newStaff[ind].staff_id = staff_id
        setStaff(newStaff)
    }
    // 员工-编辑
    function staffChange(ind, value) {
        let newStaff = JSON.parse(JSON.stringify(staff))
        newStaff[ind].memo = value
        setStaff(newStaff)
    }
    // 员工-新增
    function staffAdd() {
        let newStaff = JSON.parse(JSON.stringify(staff))
        newStaff.push({ store_id: undefined, staff_id: undefined, mome: '' })
        setStaff(newStaff)
    }
    // 通过名称模糊查询楼盘信息
    function buildByName(e) {
        clearTimeout(time)
        time = setTimeout(() => {
            setEstateList([])
            http('estate.index', {
                filter: JSON.stringify({ name: e }),
                op: JSON.stringify({ name: 'like' })
            }).then(res => {
                if (res.code === 1) {
                    setEstateList(res.data.rows)
                }
            }).catch(err => console.log(err))
        }, 800)
    }

    function filePreview(file) {
        setPreview({
            show: !0,
            content: BASE_URL + (file.content ?? file.response.data.url)
        })
    }
    function fileConfirm() {
        let status = fileList.every(e => {
            return e.status === 'done'
        })
        if (status) {
            setImgList([...imgList, ...fileList.map(e => {
                return {
                    house_id: id,
                    type: 'image',
                    content: e.response.data.url
                }
            })])
            fileCancel()
        } else {
            notification.error({
                message: '请等待图片上传完毕'
            })
        }
    }
    function fileCancel() {
        setFileList([])
        modalHide(setUpload)
    }
    function fileDel(ind) {
        if((house2?.delfytp?.value ?? '') === '无') return setj()(!0)
        let newList = JSON.parse(JSON.stringify(imgList))
        newList.splice(ind, 1)
        setImgList(newList)
    }

    function modalShow(func, text) {
        func({ show: !0, text })
    }
    function modalHide(func) {
        func({ show: !1, text: '' })
    }

    // 计算
    function calculation(a = 0, b = 0, type) {
        let res = 0
        switch (type) {
            case 'price':
                // console.log(a,b);
                res = a * b + ''
                res = insertStr(res, res.length - 4, '.')
                res = parseFloat(res)
                break;
            case 'rental':
                res = a / b
                break;
            default:
                break;
        }
        if (isNaN(res) || res === Infinity) return 0
        return res
    }
    function insertStr(soure, start, newStr) {
        return soure.slice(0, start) + newStr + soure.slice(start);
    }
}

export default Edit;